package com.example.myapplication

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class test07_Button_three_cilick : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_test07_button_three_cilick)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        // 实现点击事件方式二(必须放到onCreate函数中):
        val btn = findViewById<View>(R.id.btn222)
        btn.setOnClickListener {
            Toast.makeText(this, "点击事件222", Toast.LENGTH_SHORT).show()
        }
        // 实现点击事件方式三(获取按钮必须放到onCreate函数中):
        val btn3 = findViewById<View>(R.id.btn333)
        btn3.setOnClickListener(this) // 设置监听器为当前 Activity
    }

    // 实现点击事件方式一(必须放到AppCompatActivity类中):
    fun onBtnClick(view: View) {
        Toast.makeText(this, "点击事件111", Toast.LENGTH_SHORT).show()
    }

    // 实现点击事件方式三(必须放到AppCompatActivity类中编写实现点击事件方法):
    override fun onClick(v: View?) {
        // 需要通过 view.id 来区分不同的按钮。
        when (v?.id) {
            R.id.btn333 -> Toast.makeText(this, "点击事件333", Toast.LENGTH_SHORT).show()
        }
    }
}
